package com.ruoyi.web.controller.work;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.flowable.api.domain.WorkSign;
import com.ruoyi.flowable.api.domain.vo.WorkSignVo;
import com.ruoyi.work.service.IWorkSignService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotNull;
import java.util.List;

/**
 * 签署Controller
 *
 * @author fengcheng
 * @date 2025-03-18
 */
@RestController
@RequestMapping("/work/sign")
@Validated
public class WorkSignController extends BaseController {
    @Autowired
    private IWorkSignService workSignService;

    /**
     * 提交审核
     *
     * @param signId
     * @return
     */
    @PreAuthorize("@ss.hasPermi('work:sign:submit')")
    @Log(title = "隐患上报", businessType = BusinessType.UPDATE)
    @PutMapping("/submit/{signId}")
    public AjaxResult submit(@NotNull(message = "signId为空") @PathVariable("signId") Long signId) throws Exception {
        return toAjax(workSignService.submit(signId));
    }

    /**
     * 撤销审核
     *
     * @param signId
     * @return
     */
    @PreAuthorize("@ss.hasPermi('work:sign:revoke')")
    @Log(title = "隐患上报", businessType = BusinessType.UPDATE)
    @PutMapping("/revoke/{signId}")
    public AjaxResult revoke(@NotNull(message = "signId为空") @PathVariable("signId") Long signId) throws Exception {
        return toAjax(workSignService.revoke(signId));
    }

    /**
     * 重新申请
     *
     * @param signId
     * @return
     */
    @PreAuthorize("@ss.hasPermi('work:sign:resubmit')")
    @Log(title = "隐患上报", businessType = BusinessType.UPDATE)
    @PutMapping("/reapply/{signId}")
    public AjaxResult reapply(@NotNull(message = "signId为空") @PathVariable("signId") Long signId) throws Exception {
        return toAjax(workSignService.reapply(signId));
    }

    /**
     * 查询签署列表
     */
    @PreAuthorize("@ss.hasPermi('work:sign:list')")
    @GetMapping("/list")
    public TableDataInfo list(WorkSignVo workSignVo) {
        startPage();
        List<WorkSign> list = workSignService.selectWorkSignList(workSignVo);
        return getDataTable(list);
    }

    /**
     * 导出签署列表
     */
    @PreAuthorize("@ss.hasPermi('work:sign:export')")
    @Log(title = "签署", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, WorkSignVo workSignVo) {
        startPage();
        List<WorkSign> list = workSignService.selectWorkSignList(workSignVo);
        TableDataInfo dataTable = getDataTable(list);
        ExcelUtil<WorkSign> util = new ExcelUtil<WorkSign>(WorkSign.class);
        util.exportExcel(response, (List<WorkSign>) dataTable.getRows(), "签署数据");
    }

    /**
     * 获取签署详细信息
     */
    @PreAuthorize("@ss.hasPermi('work:sign:query')")
    @GetMapping(value = "/{signId}")
    public AjaxResult getInfo(@PathVariable("signId") Long signId) {
        return success(workSignService.selectWorkSignBySignId(signId));
    }

    /**
     * 新增签署
     */
    @PreAuthorize("@ss.hasPermi('work:sign:add')")
    @Log(title = "签署", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody WorkSignVo workSignVo) {
        return toAjax(workSignService.insertWorkSign(workSignVo));
    }

    /**
     * 修改签署
     */
    @PreAuthorize("@ss.hasPermi('work:sign:edit')")
    @Log(title = "签署", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody WorkSignVo workSignVo) {
        return toAjax(workSignService.updateWorkSign(workSignVo));
    }

    /**
     * 删除签署
     */
    @PreAuthorize("@ss.hasPermi('work:sign:remove')")
    @Log(title = "签署", businessType = BusinessType.DELETE)
    @DeleteMapping("/{signIds}")
    public AjaxResult remove(@PathVariable Long[] signIds) {
        return toAjax(workSignService.deleteWorkSignBySignIds(signIds));
    }
}
